package com.itheima.treemap;

import com.itheima.domain.Student;

import java.util.*;

/**
 * - 创建一个TreeMap集合,键是学生对象(Student),值是籍贯(String),学生属性姓名和年龄,按照年龄进行排序并遍历
 * - 要求按照学生的年龄进行排序,如果年龄相同则按照姓名进行排序
 */
public class Test02 {

    public static void main(String[] args) {
        //创建集合
        /*TreeMap<Student,String> map= new TreeMap<>(new Comparator<Student>() {
            @Override
            public int compare(Student o2, Student o1) {
                return o1.getAge()-o2.getAge()==0?o1.getName().compareTo(o2.getName()):o1.getAge()-o2.getAge();
            }
        });*/
        TreeMap<Student, String> map = new TreeMap<>((o1, o2) -> o2.getAge() - o1.getAge() == 0 ?
                o2.getName().compareTo(o1.getName()) : o2.getAge() - o1.getAge());
        //创建学生对象
        Student s1 = new Student("鹿晗", 30);//太原
        Student s2 = new Student("鹿晗", 30);//北京
        Student s3 = new Student("关晓彤", 23);//北京
        Student s4 = new Student("吴亦凡", 30);//广州
        Student s5 = new Student("蔡徐坤", 22);//温州
        Student s6 = new Student("黄子韬", 27);//青岛

        //向集合中添加元素
        map.put(s1, "太原");
        map.put(s2, "北京");
        map.put(s3, "北京");
        map.put(s4, "广州");
        map.put(s5, "温州");
        map.put(s6, "青岛");

        //遍历集合
        //获取所有键
        Set<Student> students = map.keySet();
        Iterator<Student> iterator = students.iterator();
        while (iterator.hasNext()){
            Student next = iterator.next();
            String v = map.get(next);

        }


        for (Student student : students) {
            String jiaXiang = map.get(student);
            System.out.println(student + "========" + jiaXiang);
        }
        System.out.println("----------------------------------------");
        //通过键值对
        Set<Map.Entry<Student, String>> entries = map.entrySet();
        for (Map.Entry<Student, String> entry : entries) {
            Student student = entry.getKey();
            String jiaXiang = entry.getValue();
            System.out.println(student + "========" + jiaXiang);
        }
        System.out.println("_>----------------------------------------");
        //foreach
        map.forEach((k, v) -> System.out.println(k + "===" + v));

        LinkedList list= new LinkedList();
    }
}
